things="onething,twothings,threethings,fourthings"给定这个输入,我如何用逗号分割一个字符串,然后修剪它周围的空白?结果:things=["onething","twothings","threethings","fourthings"]目前我有这个:things=things.to_s.tr("\n\t","").strip.split(/,/)这完成了大部分我想要它做的事情,除了在它以逗号分隔时删除前导/尾随空格。实现这一目标的最佳方法是什么?我想将其作为此表达式的一部分来执行,而不是将上述结果分配给单独的数组并对其进行迭代。
选择有意义。但是有人可以向我解释.detect吗?我不明白这些数据。>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,4)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,6)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,7)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(2,7)}=>2>>[1,2,3,4,5,6,7].detect{|x|x.between?(1,7)}=>1>>[1,2,3,4,5,6,7].detect{|x